package cn.edu.scut.fsearch.user;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import cn.edu.scut.fsearch.comm.comm;
import cn.edu.scut.kapok.search.comm.SearchOption.EduSE;
import cn.edu.scut.kapok.search.comm.SearchOption.SourceType;
import cn.edu.scut.kapok.search.comm.SearchOption.UniversitySE;
import cn.edu.scut.kapok.search.comm.SearchOption.TopicSE;

public class UserSearchOption {
	private SourceType type;
	private ArrayList<EduSE> eduSE;
	private ArrayList<UniversitySE> universitySE;
	private TopicSE topicSE;
	
	public boolean getPreOptions(HttpServletRequest request)
	{
		UserSearchOption preOption;
		if((preOption = (UserSearchOption)request.getSession().getAttribute("preOption")) != null)
		{
			this.type = preOption.getType();
			this.eduSE = preOption.getEduSE();
			this.universitySE = preOption.getUniversitySE();
			this.topicSE = preOption.getTopicSE();
			return true;
		}
		else{
			this.type = SourceType.v4Andv6;
			this.eduSE = new ArrayList<EduSE>(Arrays.asList(comm.eduSEs));
			this.universitySE = new ArrayList<UniversitySE>(Arrays.asList(comm.universitySEs));
			//this.topicSE = new ArrayList<TopicSE>(Arrays.asList(comm.topicSEs));
			this.topicSE = TopicSE.SCUTEDUNEWS;
			return false;
		}
	}
	
	public UserSearchOption() {
		// TODO Auto-generated constructor stub
	}
	
	public UserSearchOption(SourceType type,ArrayList<EduSE> eduList,ArrayList<UniversitySE> unList,TopicSE topicList) {
		// TODO Auto-generated constructor stub
		this.type = type;
		this.eduSE = new ArrayList<EduSE>(eduList);
		this.universitySE = new ArrayList<UniversitySE>(unList);
		this.topicSE = topicList; 
	}
	
	public UserSearchOption(String type,String []  eduList,String [] unList,String topicList) {
		// TODO Auto-generated constructor stub
		if(type == null || type.equals(""))
			this.type = SourceType.v4Andv6;
		else this.type = comm.types[Integer.parseInt(type)];

		this.eduSE = new ArrayList<EduSE>();
		if(eduList != null) {
			for (String string : eduList) {
				if(string != null && !string.equals(""))
					this.eduSE.add(comm.eduSEs[Integer.parseInt(string)]);
			}
		}
		
		this.universitySE = new ArrayList<UniversitySE>();
		if(unList != null )
		{
			for (String string2 : unList) {
				if(string2 != null && !string2.equals(""))
					this.universitySE.add(comm.universitySEs[Integer.parseInt(string2)]);
			}
		}
		
		/*this.topicSE = new ArrayList<TopicSE>();
		if(topicList != null ) {
			for (String string2 : topicList) {
				if(string2 != null && !string2.equals(""))
					this.topicSE.add(comm.topicSEs[Integer.parseInt(string2)]);
			}
		}*/
		if(topicList == null || topicList.equals("") )
			this.topicSE = null;
		else this.topicSE = comm.topicSEs[Integer.parseInt(topicList)];
		
	}

	public SourceType getType() {
		return type;
	}

	public ArrayList<EduSE> getEduSE() {
		return eduSE;
	}

	public ArrayList<UniversitySE> getUniversitySE() {
		return universitySE;
	}
	
	public TopicSE getTopicSE() {
		return topicSE;
	}
	
	public String getEduSeString(List<EduSE> ese)
	{
		int size = ese.size();
		if(size <=0)
			return "";
		int[] eduse = new int[size];
		for(int i=0;i<size;i++)
			eduse[i] = ese.get(i).ordinal();
		String result = Arrays.toString(eduse);
		result = result.substring(1,result.length()-1);
		return result;
	}
	
	public String getTopicString(TopicSE topic)
	{
		/*int size = topic.size();
		if(size <=0)
			return "";
		int[] eduse = new int[size];
		for(int i=0;i<size;i++)
			eduse[i] = topic.get(i).ordinal();
		String result = Arrays.toString(eduse);
		result = result.substring(1,result.length()-1);
		return result;*/
		if(topic == null)
			return "";
		return String.valueOf(topic.ordinal());
	}
	
	public String getUniversitySeString(List<UniversitySE> use)
	{
		int size = use.size();
		if(size <=0)
			return "";
		int[] eduse = new int[size];
		for(int i=0;i<size;i++)
			eduse[i] = use.get(i).ordinal();
		String result = Arrays.toString(eduse);
		result = result.substring(1,result.length()-1);
		return result;
	}
}
